<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace Shop\Controller;

use Admin\Api\DatatreeApi;
use Common\Api\AccountApi;
use Common\Api\BoyeServiceApi;
use Shop\Api\HasIdcodeApi;
use Shop\Api\WalletApi;
use Shop\Api\WalletHisApi;
use Shop\Api\WithdrawApi;
use Shop\Model\OrdersModel;
use Uclient\Model\OAuth2TypeModel;
use Shop\Api\CategoryApi;
use Shop\Api\OrdersInfoViewApi;
use Shop\Api\OrdersExpressApi;
use Shop\Api\ProductApi;
use Shop\Api\AddressApi;
use Tool\Api\ProvinceApi;
use Tool\Api\CityApi;
use Tool\Api\AreaApi;
use Shop\Api\OrdersApi;
use Shop\Api\OrdersItemApi;
use Admin\Api\MemberApi;
use Admin\Api\SecurityCodeApi;
use Uclient\Api\UserApi;
use Shop\Api\MemberConfigApi;
use Shop\Api\OrderRefundApi;
use Admin\Api\UidMgroupApi;
use Shop\Api\StoreApi;


class UserController extends ShopController{
	/**
	 * 
	 * 访问前判断是否登录
	 */
	
	public function _initialize(){
        parent::_initialize();
        $this->checkLogin();
    }
	
	/**
	 * 个人信息
	 */	
	public function info(){

		$this->assign('phone',substr_replace($this->userinfo['mobile'],'****', 3, 4));

		$this->assign('group',$resultw['info']);
		$this->theme($this->themeType)->display();
	}
	/*
	 * 获取市
	 * */
	public function getcity(){
		$sno=I('sno',0);
		if($sno!=0){
			$map=array('father'=>$sno);
			$result=apiCall(CityApi::QUERY_NO_PAGING,array($map));
			$this->ajaxReturn($result['info'],'json');
		}
	}	
	
	/*
	 * 分佣管理
	 * */
	public function distribution(){
        $this->checkLogin();
        $uid = $this->userinfo['id'];
        $result = apiCall(WalletApi::GET_INFO_If_NOT_EXIST_THEN_ADD,array($uid));
        if($result['status']){
            $this->assign("wallet",$result['info']);
        }
		$map=array(
            'uid'=>$uid,
            'dtree_type'=>getDatatree("COMMISSION_ORDER_PAID")
        );

		$page = array('curpage'=>I('post.p',0),'size'=>10);
		$result=apiCall(WalletHisApi::QUERY,array($map,$page));

        $this->assign('jilu',$result['info']['list']);
		$this->assign('show',$result['info']['show']);

        $result = apiCall(DatatreeApi::GET_INFO,array(array('id'=>getDatatree("COMMISSION_ORDER_PAID"))));

        if($result['status']){
            $this->assign("type_name",$result['info']['name']);
        }
		$this->theme($this->themeType)->display();
	}
//	
	/*
	 * 实名认证
	 * */
	public function truename(){

		$this->assign('phone',substr_replace($this->userinfo['mobile'],'****', 3, 4));
        $this->assign('idnumber',substr_replace($this->userinfo['idnumber'],'********', 3, 8));

        $uid = $this->userinfo['uid'];
		if(IS_GET){
			$id=array('uid'=>$uid);
			$results=apiCall(MemberConfigApi::QUERY_NO_PAGING, array($id));
			$this->assign('cfg',$results['info'][0]);
			$this->theme($this->themeType)->display();
		}else{

			$id=array('uid'=>$uid);
			$entity=array(
                'realname'=>I('post.realname',''),
                'idnumber'=>I('post.idnumber','')
            );

            $this->userinfo = array_merge($this->userinfo,$entity);
			$result=apiCall(MemberApi::SAVE_BY_ID, array($id,$entity));
            $this->checkApiCallResult($result,false);

            $entity=array('identity_validate'=>1);
            $this->userinfo = array_merge($this->userinfo,$entity);
            $result = apiCall(MemberConfigApi::SAVE_BY_ID, array($id,$entity));

			if($result['status']){
                $this->updateUserinfo($this->userinfo);
				$this->success('正在提交审核!');
			}else{
				$this->error('出错了!,请重新填写!');
			}
		}
		
	}


	/**
	 * 收货地址
	 */	
	public function address(){

        $uid = $this->userinfo['uid'];
		if(IS_GET){
			$pro=apiCall(ProvinceApi::QUERY_NO_PAGING,array());
			$this->assign('province',$pro['info']);
            $result=apiCall(AddressApi::QUERY_NO_PAGING,array(array('uid'=>$uid)));
            $this->assign('address',$result['info']);
            $this->assign('default_address',$this->userinfo['default_address']);
            $this->theme($this->themeType)->display();
		}
	}

	/**
	 * 设置默认地址
	 */
	public function setdefault(){
        $addr_id = I('param.id','');
        if(empty($addr_id)){
            $this->error("参数错误!");
        }

        $result = apiCall(MemberConfigApi::SAVE_BY_ID,array($uid,array('default_address'=>$addr_id)));
		if($result['status']){
            $this->success('设置成功!');
	    }else{
            $this->error("设置失败!");
        }
    }

	/*
	 * 忘记密码
	 * */
	public function forgetpwd1(){
		$this->theme($this->themeType)->display();
	}

	public function forgetpwd2(){
		$map=array('username'=>I('uname',''));

		$this->assign('phone',substr_replace($this->userinfo['info']['mobile'],'****', 3, 4));

		$this->theme($this->themeType)->display();
	}


	/**
	 * 个人中心
	 */
	public function index(){
        $this->checkLogin();
        $user = $this->userinfo;
		$uid=$user['id'];
		if($uid != 0){

			$this->assign('phone',substr_replace($this->userinfo['mobile'],'****', 3, 4));

			$map1=array('uid'=>$uid,'pay_status'=>0);
			$result1=apiCall(OrdersApi::QUERY_NO_PAGING,array($map1));

			$map2=array('uid'=>$uid,'order_status'=>4);
			$result2=apiCall(OrdersApi::QUERY_NO_PAGING,array($map2));

			$this->assign('wait',count($result1['info']));
			$this->assign('shouhuo',count($result2['info']));
			$this->assign('pingjia',count($result3['info']));

			$this->theme($this->themeType)->display();
		}else{
			$this->error('获取用户信息失败,请重新登录',U('Shop/Index/login'));
		}
		
	}

	/*
	 * 用户信息
	 * */
	public function infoview(){

		$this->assign('phone',substr_replace($this->userinfo['mobile'],'****', 3, 4));
		$this->assign('mobile',$user['mobile']);
		$this->theme($this->themeType)->display();
	}
	/**
	 * 修改用户资料
	 */
	public function edit(){

        $uid = $this->userinfo['uid'];
        $birthday = I('post.birthday');
        $birthday = strtotime($birthday);

		$entity=array(
			'birthday'=>$birthday,
			'nickname'=>I('post.nickname',''),
			'sex'=>I('post.sex',0),
		);

		$result=apiCall(MemberApi::SAVE_BY_ID,array($uid,$entity));

        if($result['status']){
            $this->userinfo = array_merge($this->userinfo,$entity);
            $this->updateUserinfo($this->userinfo);
			$this->success('修改成功!');
        }else
        {
            $this->error("修改失败!");
        }


		
		
	}
	/*
	 * 资金管理
	 * */
	public function moneymanager(){

		if(IS_GET){

			$map=array('uid'=>$this->userinfo['id']);

			$page = array('curpage'=>I('post.p',0),'size'=>10);

			$result=apiCall(WithdrawApi::QUERY,array($map,$page));
			$this->assign('list',$result['info']['list']);
			$this->assign('show',$result['info']['show']);


			$pro=apiCall(ProvinceApi::QUERY_NO_PAGING,array());
			$this->assign('phone',substr_replace($this->userinfo['mobile'],'****', 3, 4));
			$this->assign('pro',$pro['info']);

			$this->theme($this->themeType)->display();
		}else{


		}
		
	}
	/*
	 * 退货
	 * */
	public function resend(){
		$entity=array('create_time'=>time(),'reason'=>I('yy','无'),'valid_status'=>0,'reply_msg'=>'','order_code'=>I('orderid',0));
		$result=apiCall(OrderRefundApi::ADD,array($entity));
		if($result['status']){
			$map=array('order_code'=>I('orderid',0));
			$ent=array('order_status'=>9);
			$resu=apiCall(OrdersApi::SAVE,array($map,$ent));
			$this->success('申请退货成功,等待客服反馈!');
		}else{
			$this->error('申请退货失败!');
		}
	}

    public function order(){
        //1. 待支付订单统计
        $uid = $this->userinfo['uid'];
        $map = array(
            'uid'=>$uid,
            'order_status'=>OrdersModel::ORDER_TOBE_CONFIRMED,
            'pay_status'=>OrdersModel::ORDER_TOBE_PAID,
        );
        $result = apiCall(OrdersApi::COUNT,array($map));
        if($result['status']){
            $this->assign("tobePaidNum",$result['info']);
        }
        //1. 已发货订单统计
        $map = array(
            'uid'=>$uid,
            'order_status'=>OrdersModel::ORDER_SHIPPED
        );
        $result = apiCall(OrdersApi::COUNT,array($map));
        if($result['status']){
            $this->assign("shippedNum",$result['info']);
        }


        $this->theme($this->themeType)->display();
    }

	/*
	 * qq
	 * */
	public function zixun(){

		$this->theme($this->themeType)->display();
	}
	/**
	 * 分享
	 */
	public function show(){

		$this->theme($this->themeType)->display();
	}
	/*
	 * 我的等级
	 * */
	public function mylevel(){

		$uid=$this->userinfo['id'];

		$result=apiCall(UidMgroupApi::QUERY_WITH_STORE_AND_GROUP_INFO,array($uid));
        $this->checkApiCallResult($result);
		$this->assign('level',$result['info']);
		$this->theme($this->themeType)->display();
	}




        /*
         * 验证码验证
         */
       public function checkCode(){
       	   $user=session('user');
           $code=I('code',0);
           $mobile=I('mobile',0);
           $map=array(
               'code'=>$code,
               'accepter'=>$mobile,
               'endtime'=>array('GT',time()),
               'status'=>0,
           );
           $order="endtime desc";
           $result=apiCall(SecurityCodeApi::QUERY_NO_PAGING,array($map,$order)); 
           if($result['info']==null){
               $this->error("验证失败，验证码过期或者填写错误");
           }else{
               $map=array(
                   'code'=>$code,
                   'accepter'=>$mobile,
                   'status'=>1,
               );
			   $id=$user['id'];
			   $entity=array('mobile'=>I('newmobile',0));
			   $result3=apiCall(UserApi::SAVE_BY_ID,array($id,$entity));
               $result2=apiCall(SecurityCodeApi::SAVE_BY_ID,array($result['info'][0]['id'],$map));
               if($result2['status']){
                   $this->success("修改成功");
               }else{
                   $this->error("修改失败");
               }

           }
       }

    /**
     *  我的优惠码
     */
    public function idcode(){
        $map = array(
            'uid'=>$this->userinfo['uid']
        );
        $page = array('curpage'=>I('get.p',0),'size'=>10);
        $result = apiCall(HasIdcodeApi::QUERY,array($map,$page));

        if($result['status']){
            $this->assign("list",$result['info']['list']);
            $this->assign("show",$result['info']['show']);
        }

        $this->theme($this->themeType)->display();

    }

}

